<template>
  <view class="addres_con">
    <view class="check_address_box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" @click="chooseAddress">请选择收货地址+</button>
    </view>
    <view class="address_info" @click="changeAddress" v-else>
      <view class="row1">
        <view class="people">
          收货人: {{address.userName}}
        </view>
        <view class="phone">
          电话: {{address.telNumber}}
        </view>
      </view>
      <view class="row2">
        <view class="address_title">
          收货地址:
        </view>
        <view class="address_detail">
          {{addressStr}}
        </view>
      </view>
    </view>
    <image src="/static/cart_border@2x.png" class="img_border"></image>
  </view>
</template>

<script>
  import {mapState, mapMutations, mapGetters } from 'vuex'
  export default {
    name: "my_address",
    data() {
      return {
      };
    },
    computed:{
      // 收货地址vuex数据
      ...mapState('m_user',['address']),
      // getter收货地址拼接地址
      ...mapGetters('m_user',['addressStr'])
    },
    methods: {
      // 更新收货地址方法
        ...mapMutations('m_user',['updateAddress']),
      // 选择收货地址
      async chooseAddress() {
        // 1. 调用小程序提供的 chooseAddress() 方法，即可使用选择收货地址的功能
        //    返回值是一个数组：第 1 项为错误对象；第 2 项为成功之后的收货地址对象
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        // 2. 用户成功的选择了收货地址
        if (err === null && succ.errMsg === 'chooseAddress:ok') {
          // 为 data 里面的收货地址对象赋值
          this.updateAddress(succ)
        }
      },
      // 重新选择收货地址
      changeAddress(){
        this.chooseAddress()
      }
    }
  }
</script>

<style lang="scss">
  .addres_con {
    .check_address_box {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 70px;
    }

    .address_info {
      font-size: 12px;
      padding: 10px 5px;

      .row1 {
        display: flex;
        justify-content: space-between;
      }

      .row2 {
        .address_title {
          white-space: nowrap;
          margin-right: 5px;
        }

        display: flex;
        margin-top: 10px;
        align-items: center;
      }
    }

    .img_border {
      display: block;
      width: 100%;
      height: 5px;
    }
  }
</style>
